﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web.UI;
using ProjectBase.Core;
using ProjectBase.Core.Web;
using ProjectBase.Mvc;
using ProjectBase.Mvc.Controllers;
using Survey.Common;
using Survey.Common.Business;
using Survey.Common.Controllers;
using Survey.Common.Models;
using Survey.Entity;

namespace Survey.Controllers
{
    /// <summary>
    /// All actions for Vote
    /// </summary>
    public class VoteController : WebController<IVoteModel>, IVoteController
    {
        #region Actions

        [OutputCache(Duration = 60, Location = OutputCacheLocation.Any)]
        [HttpPost]
        public override ActionResult Index()
        {
            return RedirectToAction(Str.Actions.Default, Str.Controllers.Home);
        }

        #endregion Actions

        [OutputCache(Duration = 60, Location = OutputCacheLocation.Any)]
        [Transaction]
        public virtual ActionResult Index(int? id, int? answerId)
        {
            if (id.Is() && answerId.Is())
            {
                var question = QuestionFacade.GetById(id.Value);

                if (question.Is() && question.Answers.Count > answerId.Value)
                {
                    question.Answers[answerId.Value].IncreaseCount();
                    QuestionFacade.Update(question);
                }
            }
            return RedirectToAction(Str.Actions.Default, Str.Controllers.Home);
        }

        public virtual ActionResult Questions()
        {
            var filter = new QuestionFilter
            {
                ValidUntilFrom = DateTime.Now.Date,
                IsApproved = true,
            };
            Model.Questions = QuestionFacade.Find(filter);
            return PartialView("~/Controls/Examples/Survey/Question.cshtml", Model);
        }

        #region properties

        public override string ControllerName { get { return Str.Controllers.Vote; } }

        public virtual IQuestionFacade QuestionFacade { get; set; }

        #endregion properties
    }
}